<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>配置发放名单</title>
<jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<script>
var importGrid;
var _cpId = ${cpId};
var resultList = ${resultList};
console.log(_cpId, resultList);
$(function () {
  $("#import").ligerGrid({
    checkbox: true,
    columns: [
      {
        display: '主键',
        name: 'id',
        width: 60,
        align: 'left'
      },
      {
        display: '客户名称',
        name: 'cstNm'
      },
      {
        display: '客户id',
        name: 'cstId'
      },
      {
        display: '用户id',
        name: 'usrId'
      },
      {
        display: '手机号',
        name: 'mp',
      },
      {
        display: '身份证号',
        name: 'certNo'
      }
    ],
    data: { Rows: resultList },
    width: '100%',
    height: '80%',
    isChecked: f_isChecked,
    onCheckRow: f_onCheckRow,
    onCheckAllRow: f_onCheckAllRow
  });
  importGrid = liger.get("import");
});
function f_onCheckAllRow(checked) {
  for (var rowid in this.records) {
    if (checked)
      addCheckedCustomer(this.records[rowid]['id']);
    else
      removeCheckedCustomer(this.records[rowid]['id']);
  }
}

/*
 * 该例子实现 表单分页多选
 * 即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/
var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
  for (var i = 0; i < checkedCustomer.length; i++) {
    if (checkedCustomer[i] == CustomerID) return i;
  }
  return -1;
}
function addCheckedCustomer(CustomerID) {
  console.log(CustomerID)
  if (findCheckedCustomer(CustomerID) == -1)
    checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
  var i = findCheckedCustomer(CustomerID);
  if (i == -1) return;
  checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
  if (findCheckedCustomer(rowdata.cstId) == -1)
    return false;
  return true;
}
function f_onCheckRow(checked, data) {
  if (checked) addCheckedCustomer(data.cstId);
  else removeCheckedCustomer(data.cstId);
}

/*
 * 批量删除
 * start
 */
function deleteNameList() {
  if (!checkedCustomer.length) {
    $.ligerDialog.SQalert({
      content: '请先选中要删除的名单',
      SQ_type: "warning"
    });
    return
  }
  $.ligerDialog.confirm('确定删除选中名单吗?', function (flag) {
    if (flag == false) return;
    var l = checkedCustomer.join(',')
    var url = '<%=path%>/mcr/mcrCouponMain/delete.do';
    AjaxUtils.ajaxPost(url, { cstIds:  l, cpId: _cpId }, delCallback);
  })
}

function delCallback(msg) {
  console.log(msg)
  if (msg.success == 1) {
    checkedCustomer.forEach(function (i) {
      var index = resultList.findIndex(item => item.cstId == i)
      resultList.splice(index, index + 1)
    })
    UIUtils.alertSuccess(msg.info);
    checkedCustomer = []
    importGrid.reload();
  } else {
    UIUtils.alertError(msg.info);
  }
}
/* end 批量删除 end */

function downloadExcel() {
  AjaxUtils.ajaxPost('<%=path%>/mcr/mcrCouponMain/download.do', {},  downloadExcelCallback);
}

function downloadExcelCallback (msg) {
  if (msg.success == 1) {
    window.open(msg.data);
  } else {
    UIUtils.alertError(msg.info);
  }
};

/* 
 * 导入名单
 * start
 */
function fileuploadChange (e) {
  var file = e.target.files[0];
  $('#filePath').text(file.name)
}

function upload (e) {
  if (e.target.files.length === 0) {
    $.ligerDialog.SQalert({
      content: '请选择导入文件',
      SQ_type: "warning"
    });
    return
  }
  var file = e.target.files[0];
  $.ligerDialog.confirm('确定名单吗?', function (flag) {
    if (flag == false) return;
    var url = '<%=path%>/mcr/mcrCouponMain/delete.do';
    AjaxUtils.ajaxPost(url, { redidList: checkedCustomer }, importCallback);
  })
}

function importCallback(msg) {
  if (msg.success == 1) {
    checkedCustomer = []
    resultList.forEach(function(item, i) {
      resultList.splice(i, i+1);
    })
    msg.data.forEach(item => {
      resultList.push(item)
    })
    importGrid.reload();
    UIUtils.alertSuccess(msg.info);
  } else {
    UIUtils.alertError(msg.info);
  }
}


function uploadBtnEvent() {
  //获取上传文件地址
  var files = getUploadResultParams();
  if (files && files.length > 0) {
    var filePath = files[0].filePath;
    var fileExtension = filePath.substring(filePath.lastIndexOf('.') + 1);
    if (fileExtension !== 'xls' && fileExtension !== 'XLS' && fileExtension !== 'xlsx' && fileExtension !== 'XLSX') {
      UIUtils.alertWarning("请上传.xls或.xlsx后缀的文件!");
      return;
    }

    var _url = '<%=path%>/mcr/mcrCouponMain/import.do';
    AjaxUtils.ajaxPost(_url, {filePath: filePath, cpId: _cpId}, function (msg) {
      console.log(msg, '上传配置客户数据')
      if (msg.success === 1) {
        UIUtils.alertSuccess(msg.info);
      } else {
        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
      }
    }, true);
  } else {
    UIUtils.alertWarning("请先上传导入文件!");
  }
}

/* end 导入名单 end */
</script>

<body class="iframeBody">
<div class="Wrapper" style="margin: 0 auto; width: 90%; height: 90%;">
  <header class="searchArea" style="margin: 15px 0">
    <form id="formImport" class="formBtns" style="float: none; line-height: 2;">
      <span id="clearBtn" onclick="deleteNameList()" class="sq-button-round" style="vertical-align:top;">删除</span>

      
      <span id="clearBtn" onclick="downloadExcel();" class="sq-button-round" style="vertical-align:top;">下载模版</span>

      <div class="fieldBox clearFix" style="display: inline-block;">
        <div class="field">
          <jsp:include page="../../system/sysUlFilesRec/sysSysUlFilesRecUpload.jsp" flush="true" >
            <jsp:param name="fileNum" value="1"/>
            <jsp:param name="filePath" value="mcrCouponMain/temp/"/>
          </jsp:include>

          <span id="upBtn" onclick="uploadBtnEvent();" class="sq-button-round">导入</span>
        </div>
      </div>

      <!-- <span class="sq-button sq-button-round fileupload-button" style="vertical-align:top;">选择文件
        <input type="file" id="fileupload" multiple name="fileupload" class="fileupload-input" onchange="fileuploadChange(event)" accept=".csv, .xlsx, .xls" />
      </span>
      <span id="filePath"></span>
      <span id="uploadBtn" onclick="upload();" class="sq-button-round" style="vertical-align:top;">导入</span> -->
    </form>
  </header>
  <div id="import"></div>
</div>
</body>
</html>